/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: MIT
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-11-24     XiaoYi       the first version
 */

#ifndef __MID_CAT1_H__
#define __MID_CAT1_H__

#include <rtthread.h>
#include "main.h"

#define UART_RECV_MAX_SIZE    128

/*************************************** MQTT初始化 ***************************************/
typedef struct _cat1_params
{
    char *host;
    int port;
    char *client_id;
    int keepAlive;
    char *user_name;
    char *password;
    int clean;
	char *version;
    int encrypt;
}cat1_mqtt_params_t;

/************************************ 定义系统回调函数 ************************************/
typedef enum{
    CAT1_STATUS_INIT,
    CAT1_STATUS_CONNECTED,
    CAT1_STATUS_SOCKET_CONNECTING,
    CAT1_STATUS_CONNECT_REQUEST,
    CAT1_STATUS_WAIT_ACK,
	CAT1_STATU_PING,
}cat1_status_e;

// 数据接收
typedef void (*cat1_recv_fun)(char *data, int len);

// 接收状态数据 心跳包、 上线 下线 重连
typedef void (*cat1_status_fun)(cat1_status_e status);

typedef struct _cat1_ctx_t{
	char data_buf[UART_RECV_MAX_SIZE];
	cat1_recv_fun recv_cb;
	int status;   //心跳包、 上线 下线 重连
	cat1_status_fun status_cb;
}cat1_ctx_t;

/************************************ 处理串口接收数据 ************************************/

typedef struct
{
    volatile uint8_t full_packet; // 1：one frame of complete packet
    volatile uint8_t count;
    uint8_t uart_recv_buff[UART_RECV_MAX_SIZE];
}data_frame_t;
	
extern data_frame_t mid_cat1_data;

void mid_cat1_ctx_register(cat1_ctx_t *ctx);

void mid_cat1_init(void);

int mid_cat1_set_ate0(void);

int mid_cat1_get_ccid(char *ccid);

int mid_cat1_get_ready_status(void);

int mid_cat1_set_apn(void);

int mid_cat1_active_pdp(void);

int mid_cat1_get_qos(void);

int mid_cat1_send_cmd (char * cmd, uint8_t cmd_len, char * reply1, char * reply2, uint32_t waittime);

int mid_cat1_mqtt_init(cat1_mqtt_params_t *params);

int mid_cat1_mqtt_start(void);

int mid_cat1_mqtt_get_status(void);

int mid_cat1_mqtt_subscribe(char *topic, int qos);

int mid_cat1_mqtt_publish(char *topic, int qos, int retain, int dup, char *message );

int mid_cat1_mqtt_disconnect(void);

int mid_cat1_mqtt_auto_reconnect(int recon);

int mid_cat1_process_uart_recv_msg(void);

#endif
